package components
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.controls.ComboBox;
	import mx.controls.ToolTip;
	import mx.controls.Tree;
	import mx.core.Application;
	import mx.events.ListEvent;
	import mx.events.ResizeEvent;
	import mx.managers.ToolTipManager;
	
	import nl.wv.extenders.panel.SuperPanel;
	
	import tools.*;
	
	public class AllCourses extends Sprite{
		private var panel:SuperPanel = new SuperPanel();
		private var c:Tree = new Tree();
		private var smjer:ComboBox = new ComboBox();
		private var tool:Tool = new Tool();
		private var course:Array = new Array();
	 	private var service:Mysql = new Mysql();
		private var service1:Mysql = new Mysql();
		private var service2:Mysql = new Mysql();
		private var temp:Course;
		private var xmlPom:String;
		private var tip:ToolTip;
		private var createToolTip:Boolean = true;
		
		public function AllCourses(p:SuperPanel){
			panel = p;
			c.x = 10;
			c.y = 34;
	 		c.height=panel.height-85;
	 		c.width=panel.width-30;
	 		smjer.x = 10;
	 		smjer.y = panel.height - 45;
	 		smjer.width = panel.width - 30;
	 		c.addEventListener(ListEvent.ITEM_ROLL_OVER, overList);
	 		c.addEventListener(ListEvent.ITEM_ROLL_OUT, outList);
			p.addChild(c);
			p.addChild(smjer);
			panel.addEventListener(ResizeEvent.RESIZE, onResize);
			service1.sql = {type:"SELECT", a1:"s.id_smjer", a2:"s.naziv", from:"smjer s", where:""};
	 		service1.addEventListener(service1.MYSQL_RESULT, onSmjerResult);
	 		service1.startService();
		}
		
		public function overList(event:ListEvent):void{
			c.selectedIndex = event.rowIndex;
			if(createToolTip && event.currentTarget.selectedItem.@tip != -1){
				var xmlList:XMLList = c.dataProvider.children();
				var tekst:String = "Naziv predmeta: "+xmlList[event.rowIndex].@naziv+"\nSifra: "+xmlList[event.rowIndex].@sifra+"\nECTS kredita: "+xmlList[event.rowIndex].@ects+"\n\nOpis: "+tool.PlainTekst(xmlList[event.rowIndex].@opis); 
	            if(panel.x+panel.width+280<panel.parentApplication.screen.width){
	            	tip = ToolTipManager.createToolTip(tekst, panel.x+panel.width-20, Application.application.parent.mouseY) as ToolTip;
             	} else tip = ToolTipManager.createToolTip(tekst, panel.x, Application.application.parent.mouseY) as ToolTip;
             	createToolTip = false;
        	    tip.setStyle("backgroundColor",0xFFCC00);
        	    tip.width = 300;
   			}
		}

		public function outList(event:ListEvent):void{
			if(event.currentTarget.selectedItem.@tip != -1){
				if(!createToolTip) ToolTipManager.destroyToolTip(tip);
				createToolTip = true;
			}
		}
		
		public function onResize(e:Event):void{
			c.height=panel.height-85;
	 		c.width=panel.width-30;
	 		smjer.y = panel.height - 45;
	 		smjer.width = panel.width - 30;
		}
		
		public function setDataProvider(xml:XML):void{
			c.dataProvider = xml;
			c.showRoot = false;
			c.showDataTips = true;
			c.labelField = "@naziv"
			c.selectedIndex = 0;
       		c.addEventListener(ListEvent.ITEM_DOUBLE_CLICK, onDoubleClick);
       		c.focusEnabled = true;
		}
		
		public function onSmjerResult(e:Event):void{
			var xmlList:XMLList = service1.xml.children();
			var xmlPom1:String = "<root naziv='Smjerovi' tip='-1'>";
			xmlPom1+="<smjer id='-1' naziv='Svi kursevi'></smjer>";
			for (var i:int=0; i<xmlList.length(); i++) {
				xmlPom1+="<smjer id='"+xmlList[i].id_smjer+"' naziv='"+xmlList[i].naziv+"'> </smjer>";
			}
			xmlPom1+="</root>";
			smjer.dataProvider = new XML(xmlPom1).children();
			smjer.labelField="@naziv";
			smjer.addEventListener(ListEvent.CHANGE, onSmjerClick);
		}
		
		public function onSmjerClick(e:ListEvent):void{
			if(e.currentTarget.selectedItem.@id!=-1) {
				service2.sql = {type:"SELECT", a1:"k.naziv", a2:"k.id_kurs", a3:"k.sifra", a4:"k.opis", a5:"k.ects", from:"kurs k", where: "k.smjer="+e.currentTarget.selectedItem.@id+" ORDER BY naziv ASC"};
	 			service2.addEventListener(service2.MYSQL_RESULT, onCoursesSmjerResult);
	 			service2.startService();
			} else {
				service2.sql = {type:"SELECT", a1:"k.naziv", a2:"k.id_kurs", a3:"k.sifra", a4:"k.opis", a5:"k.ects", from:"kurs k", where: "k.id_kurs>0 ORDER BY naziv ASC"};
	 			service2.addEventListener(service2.MYSQL_RESULT, onCoursesSmjerResult);
	 			service2.startService();	
			}
		}
		
		private function onCoursesSmjerResult(e:Event):void{ 
			var xmlList:XMLList = service2.xml.children();
			var xmlPom1:String = "<root naziv='Kursevi' tip='-1'>";
			for (var i:int; i<xmlList.length(); i++) {
				xmlPom1+="<kurs bodovi='"+xmlList[i].bodovi+"' ects='"+xmlList[i].ects+"' sifra='"+xmlList[i].sifra+"' opis='"+xmlList[i].opis+"' naziv='"+xmlList[i].naziv+"' id_kurs='"+xmlList[i].id_kurs+"' />";
			}
			xmlPom1+="</root>";
			var xml:XML=new XML(xmlPom1);
			setDataProvider(xml);
			service2.removeEventListener(service2.MYSQL_RESULT, onCoursesResult);
		}
	
		public function onDoubleClick(e:ListEvent):void{			
			if(!panel.parentApplication.checkExist(c.selectedItem.@naziv) && e.currentTarget.selectedItem.@tip != -1){
				var s:SuperPanel;
				panel.parentApplication.panels.push(s = tool.createPanel(c.selectedItem.@naziv, 0, 400, 400, 300, 0, true, false, "Kurs"));
				panel.parentApplication.addChild(s);
				s.backButton.addEventListener(MouseEvent.CLICK, panel.parentApplication.taskbarButtonClicked);
				s.backButton.addEventListener(MouseEvent.RIGHT_CLICK, panel.parentApplication.taskbarButtonRightClicked);
				course.push(temp = new Course(s));
				temp.setParameters(c.selectedItem.@id_kurs);
				service.sql = {type:"SELECT", a1:"r.naziv", a2:"r.opis", a3:"r.url", a4:"r.tip", a5:"r.vrijeme", a6:"p.ime", a7:"p.prezime", a8:"r.datum", a9:"m.id_materijal", a10:"m.kurs", a11:"m.attachments", from:"materijal m, predavac p, podesavanja s, godina g, resursi r", where:"r.id_resurs=m.id_materijal AND m.predavac=p.id_predavac AND s.godina=g.godina AND g.id_godina=m.godina AND m.kurs="+c.selectedItem.@id_kurs};
	 			service.addEventListener(service.MYSQL_RESULT, onCoursesResult);
	 			service.startService();	
			}
		}
		
		private function onCoursesResult(e:Event):void{ 
			service.removeEventListener(service.MYSQL_RESULT, onCoursesResult);
			var xmlList:XMLList = service.xml.children();
			xmlPom = "<tree><root naziv='Materijali' tip='-1'>";
			for (var i:int=0; i<xmlList.length(); i++) {
				xmlPom+="<materijal resurs='"+xmlList[i].attachments+"' kurs='"+xmlList[i].kurs+"' id='"+xmlList[i].id_materijal+"' naslov='"+xmlList[i].naziv+"' naziv='"+xmlList[i].naziv+"' opis='"+xmlList[i].opis+"' datum='"+xmlList[i].datum+"' attachments='"+xmlList[i].url+"' tip='"+xmlList[i].tip+"' vrijeme='"+xmlList[i].vrijeme+"' tekst='"+xmlList[i].url+"' ime='"+xmlList[i].ime+"' prezime='"+xmlList[i].prezime+"'><tema naziv='Ucitavanje...' /></materijal>";
			}
			xmlPom+="</root>";
			service.sql = {type:"SELECT", a1:"t.id_tema", a2:"t.naslov", a3:"t.tekst", a4:"k.username", a5:"t.datum", a6:"t.vrijeme", a7:"count(c.id_komentar)", from:"tema t, korisnik k, kurs q, komentar c", where:"t.kurs=q.id_kurs AND k.id_username=t.username AND t.id_tema=c.tema AND q.id_kurs="+c.selectedItem.@id_kurs+" AND t.materijal is null GROUP BY id_tema"+" ORDER BY t.datum DESC, t.vrijeme DESC"};
	 		service.addEventListener(service.MYSQL_RESULT, onThemesResult);
	 		service.startService();	
		}
		
		private function onThemesResult(e:Event):void{
			service.removeEventListener(service.MYSQL_RESULT, onThemesResult);
			var xmlList:XMLList = service.xml.children();
			xmlPom+="<root naziv='Teme sa odgovorima' tip='-1' >";
			for (var i:int=0; i<xmlList.length(); i++) {
				xmlPom+="<tema brojkomentara='"+xmlList[i].count_c_id_komentar_+"' tip='2' naziv='"+ (String(xmlList[i].naslov).length>25 ? String(xmlList[i].naslov).substring(0,25)+"..." : xmlList[i].naslov) + "' naslov='"+xmlList[i].naslov+"' id='"+xmlList[i].id_tema+"' tekst='"+xmlList[i].tekst+"' username='"+xmlList[i].username+"' datum='"+xmlList[i].datum+"' vrijeme='"+xmlList[i].vrijeme+"'/>";
			}
			xmlPom+="</root>";
			service.sql = {type:"SELECT", a1:"t.id_tema", a2:"t.naslov", a3:"t.tekst", a4:"k.username", a5:"t.datum", a6:"t.vrijeme", from:"tema t, korisnik k, kurs q", where:"t.kurs=q.id_kurs AND k.id_username=t.username AND t.materijal is null AND q.id_kurs="+c.selectedItem.@id_kurs+" AND t.id_tema not in (SELECT tema from komentar)"+" ORDER BY t.datum DESC, t.vrijeme DESC"};
	 		service.addEventListener(service.MYSQL_RESULT, onThemes1Result);
	 		service.startService();				
		}
		
		private function onThemes1Result(e:Event):void{
			service.removeEventListener(service.MYSQL_RESULT, onThemes1Result);
			var xmlList:XMLList = service.xml.children();
			xmlPom+="<root naziv='Teme bez odgovora' tip='-1' >";
			for (var i:int; i<xmlList.length(); i++) {
				xmlPom+="<tema tip='2' naziv='" + (String(xmlList[i].naslov).length > 25 ? String(xmlList[i].naslov).substring(0,25)+"..." : xmlList[i].naslov )+"' naslov='"+xmlList[i].naslov+"' id='"+xmlList[i].id_tema+"' tekst='"+xmlList[i].tekst+"' username='"+xmlList[i].username+"' datum='"+xmlList[i].datum+"' vrijeme='"+xmlList[i].vrijeme+"'/>";
			}
			xmlPom+="</root></tree>";
			var xml:XML=new XML(xmlPom);
			xmlPom="";
			temp.setDataProvider(xml);
		}
	}
}